// pages/goods/find/find.js
let api = require('../../../utils/api.js')
let app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    type:"",
    content: [],
    searchWord: '',
    empty:false
  },
  bindinput(e) {
    let value = e.detail.value
    this.setData({
      searchWord: value
    })
  },
  search() {
    let value = this.data.searchWord
    if (!value) {
      api.alert("请输入关键词")
      return;
    }
    if(this.data.type == 'custom'){//定制产品搜索
      this.searchCustom()
      return;
    }
    let params = {
      page:1,
      size:9999,
      spec_id:"", //规格id
      member_id:api.cache('user').member_id||"",
      craft_id:"", //工艺id
      color_id:"", //颜色id
      condition:value
    }
    api.post('goods_search',params).then(res => {
      if (res.code == 200) {
        if(res.data.goods_arr.length==0){
          this.setData({
            empty:true
          })
          api.tip("该选项下暂无相关产品")
          return;
        }
        this.setData({
          content:res.data.goods_arr,
          empty:false
        })
      }
    })
  },
  searchCustom(){
    let data = this.data;
    let condition = data.searchWord
    let params = {
      page:1,
      size:999,
      space_id:"",
      member_id:api.cache('user').member_id||"",
      condition
    }
    api.post('soft_goods_search',params).then(res => {
      if (res.code == 200) {
        this.setData({
          content:res.data.goods_arr
        })
      }
    })
  },
  toDetail(e){
    let id = e.currentTarget.dataset.id
    api.saveFoot(id,1)
    wx.navigateTo({
      url: '../detail/detail?id='+id,
    })
  },
  toSearch(e) {
    // let res = api.checkLogin()
    // if (!res) {
    //   return;
    // }
    let value = e.currentTarget.dataset.value
    console.log(value)
    //请求
    api.get('goods?keyword=' + value).then(res => {
      if (res.code == 200) {
        app.globalData.searchData = []
        app.globalData.searchData = res.data
        wx.navigateTo({
          url: '/pages/goods/search/search',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.setData({
        type:options.type
      })
    }else{
      this.setData({
        type:""
      })
    }
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  getHis() {
    //搜索过
    let arr = api.cache('historySearch')
    this.setData({
      historySearch: arr
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHis()
  },
})